package com.wwwarehouse.common.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.register.PushReceiverBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.AgainLoginEvent;
import com.wwwarehouse.common.eventbus_event.AppLoginEvent;
import com.wwwarehouse.common.eventbus_event.AuthIsOnlineEvent;
import com.wwwarehouse.common.eventbus_event.AuthScanEvent;
import com.wwwarehouse.common.eventbus_event.CardDeskRefreshEvent;
import com.wwwarehouse.common.eventbus_event.F2FInviteEvent;
import com.wwwarehouse.common.eventbus_event.GetLoginLinkEvent;
import com.wwwarehouse.common.eventbus_event.GiveOnLineEvent;
import com.wwwarehouse.common.eventbus_event.MaintainGoodsImageEvent;
import com.wwwarehouse.common.eventbus_event.MessageCenterReceivedEvent;
import com.wwwarehouse.common.eventbus_event.NotificationEvent;
import com.wwwarehouse.common.eventbus_event.PayResultEvent;
import com.wwwarehouse.common.eventbus_event.PushImportEvent;
import com.wwwarehouse.common.eventbus_event.ScanCodeEvent;
import com.wwwarehouse.common.eventbus_event.StartImportEvent;
import com.wwwarehouse.common.tools.LogUtils;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class JPushReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
        try {
			Bundle bundle = intent.getExtras();
	        String message = bundle.getString(JPushInterface.EXTRA_EXTRA);  //后台推送过来的消息
	        PushReceiverBean pushReceiverBean = JSON.parseObject(message, PushReceiverBean.class);
			LogUtils.showLog("发过来的消息：" + message);
			if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
				String registrationId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
				LogUtils.showLog("[JPushReceiver] 接收Registration Id : " + registrationId);
				//send the Registration Id to your server...

				BaseApplication.sp.putValue(Constant.sp_RegistrationID, registrationId);
			} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
				LogUtils.showLog("[JPushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
				LogUtils.showLog("[JPushReceiver] 接收到推送下来的通知");
				int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
				LogUtils.showLog("[JPushReceiver] 接收到推送下来的通知的ID: " + notificationId + " 类型： " + pushReceiverBean.getType());

				if (pushReceiverBean != null && pushReceiverBean.getType() != null) {
					if ("OPERATION".equals(pushReceiverBean.getType())) {
						//卡片桌面接受到新任务
						EventBus.getDefault().post(new CardDeskRefreshEvent(message));
					} else if ("PUSH_MESSAGE".equals(pushReceiverBean.getType())) {
						//消息中心
						EventBus.getDefault().post(new MessageCenterReceivedEvent(message));
					} else if ("faceToFace".equals(pushReceiverBean.getType())) {
						//面对面邀请
						EventBus.getDefault().post(new F2FInviteEvent(message));
					} else if ("appLogin".equals(pushReceiverBean.getType())) {
						//PC端登录
						EventBus.getDefault().post(new AppLoginEvent(message));
					} else if ("againLogin".equals(pushReceiverBean.getType())) {
						//单点登录
						EventBus.getDefault().post(new AgainLoginEvent(message));
					} else if ("startImportTool".equals(pushReceiverBean.getType()) || "startInitStock".equals(pushReceiverBean.getType())
							|| "createImportCode".equals(pushReceiverBean.getType()) || "startTaskPushType".equals(pushReceiverBean.getType())) {
						//手机转PC开始导入中计时
						EventBus.getDefault().post(new CardDeskRefreshEvent(""));
						EventBus.getDefault().post(new StartImportEvent(message));
					} else if ("completeImportTool".equals(pushReceiverBean.getType()) || "completeInitStock".equals(pushReceiverBean.getType())
							|| "completeImportDataCode".equals(pushReceiverBean.getType()) || "completeImportMap".equals(pushReceiverBean.getType())
							|| "completeImportDataDelivery".equals(pushReceiverBean.getType()) || "completeImportToolsBarCode".equals(pushReceiverBean.getType())) {
						//手机转PC端导入成功的推送
						EventBus.getDefault().post(new CardDeskRefreshEvent(""));
						EventBus.getDefault().post(new PushImportEvent(message));
					} else if ("scanCode".equals(pushReceiverBean.getType())) {
						//三角链 扫码推送
						EventBus.getDefault().post(new ScanCodeEvent(message));
					} else if ("giveOnLine".equals(pushReceiverBean.getType())) {
						//三角链 登录状态
						EventBus.getDefault().post(new GiveOnLineEvent(message));
					} else if ("getLoginLink".equals(pushReceiverBean.getType())) {
						//三角链 接受被指定命令
						EventBus.getDefault().post(new GetLoginLinkEvent(message));
					} else if ("auditInvitee".equals(pushReceiverBean.getType())) {
						//审核邀请人
						EventBus.getDefault().post(new CardDeskRefreshEvent(""));
					} else if ("authScanCode".equals(pushReceiverBean.getType())) {
						//扫码授权
						EventBus.getDefault().post(new AuthScanEvent(message));
					} else if ("authIsOnline".equals(pushReceiverBean.getType())) {
						//授权验证是否在线
						EventBus.getDefault().post(new AuthIsOnlineEvent(message));
					} else if ("maintainGoodsImager".equals(pushReceiverBean.getType())) {
						//PC端维护商品图片
						EventBus.getDefault().post(new MaintainGoodsImageEvent(message));
					} else if ("paySuccessNotification".equals(pushReceiverBean.getType())) {
						//支付成功
						EventBus.getDefault().post(new PayResultEvent("success"));
					} else if ("payFailureNotification".equals(pushReceiverBean.getType())) {
						//支付失败
						EventBus.getDefault().post(new PayResultEvent("failure"));
					} else if ("payCancelNotification".equals(pushReceiverBean.getType())) {
						//支付取消
						EventBus.getDefault().post(new PayResultEvent("cancel"));
					} else {
						
					}
				}
			} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
				LogUtils.showLog("[JPushReceiver] 用户点击打开了通知");

				//点击通知的事件，在卡片桌面去接收
				EventBus.getDefault().post(new NotificationEvent(message));
			} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
				LogUtils.showLog("[JPushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
				//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

			} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
				boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
				LogUtils.showLog("[JPushReceiver]" + intent.getAction() +" connected state change to " + connected);
			} else {
				LogUtils.showLog("[JPushReceiver] Unhandled intent - " + intent.getAction());
			}
		} catch (Exception e){

		}

	}
}
